#!/usr/bin/python3
# -*- coding: utf-8 -*-
# @who:     spacelacc
# @when:    2025/04/26
# @what:    widget for showing a cbe logo
# @vers:    V1


from PySide6.QtGui     import QPainter, QPen, QBrush, QColor
from PySide6.QtCore    import Qt, QSize, QPoint, Signal, Property
from PySide6.QtWidgets import QWidget


class CustomCubelogo(QWidget):

    signalSplashDone = Signal()

    def __init__(self, width, height, adapter, parent=None):
        super().__init__(parent)
        self.mww = width           # Main Window Width
        self.mwh = height          # Main Window Height
        self.adapter = adapter

    def paintEvent(self, event):
        painter = QPainter(self)
        painter.setRenderHint(QPainter.RenderHint.Antialiasing)
        painter.setPen(QPen(Qt.blue, 3))

        # draw background
        width = self.rect().width()
        height = self.rect().height()
        painter.fillRect(0, 0, width, height, QBrush(Qt.white))

        ww = 1.00 * self.rect().height()
        wh = ww
        cx = 0.5 * self.rect().width()
        cy = 0.5 * self.rect().height()
        self.drawCube(painter, cx, cy, ww)

    def drawCube(self, painter, cx, cy, sidelength):
        cos30 = 0.866
        cos60 = 0.50

        side = 0.25 * sidelength
        line = 0.33333 * side

        # Middle Top Part
        painter.drawLine(cx, cy-side, cx-line*cos30, cy-side+line*cos60)
        painter.drawLine(cx, cy-side, cx, cy-side+line)
        painter.drawLine(cx, cy-side, cx+line*cos30, cy-side+line*cos60)
        # Left Top Part
        painter.drawLine(cx-side*cos30, cy-side*cos60, cx-side*cos30+line*cos30, cy-side*cos60-line*cos60)
        painter.drawLine(cx-side*cos30, cy-side*cos60, cx-side*cos30+line*cos30, cy-side*cos60+line*cos60)
        painter.drawLine(cx-side*cos30, cy-side*cos60, cx-side*cos30, cy-side*cos60+line)
        # Right Top Part
        painter.drawLine(cx+side*cos30, cy-side*cos60, cx+side*cos30-line*cos30, cy-side*cos60-line*cos60)
        painter.drawLine(cx+side*cos30, cy-side*cos60, cx+side*cos30-line*cos30, cy-side*cos60+line*cos60)
        painter.drawLine(cx+side*cos30, cy-side*cos60, cx+side*cos30, cy-side*cos60+line)
        # Center Part
        painter.drawLine(cx, cy, cx-line*cos30, cy-line*cos60)
        painter.drawLine(cx, cy, cx, cy+line)
        painter.drawLine(cx, cy, cx+line*cos30, cy-line*cos60)
        # Left Bottom Part
        painter.drawLine(cx-side*cos30, cy+side*cos60, cx-side*cos30, cy+side*cos60-line)
        painter.drawLine(cx-side*cos30, cy+side*cos60, cx-side*cos30+line*cos30, cy+side*cos60-line*cos60)
        painter.drawLine(cx-side*cos30, cy+side*cos60, cx-side*cos30+line*cos30, cy+side*cos60+line*cos60)
        # Right Bottom Part
        painter.drawLine(cx+side*cos30, cy+side*cos60, cx+side*cos30, cy+side*cos60-line)
        painter.drawLine(cx+side*cos30, cy+side*cos60, cx+side*cos30-line*cos30, cy+side*cos60-line*cos60)
        painter.drawLine(cx+side*cos30, cy+side*cos60, cx+side*cos30-line*cos30, cy+side*cos60+line*cos60)
        # Middle Bottom Part
        painter.drawLine(cx, cy+side, cx-line*cos30, cy+side-line*cos60)
        painter.drawLine(cx, cy+side, cx, cy+side-line)
        painter.drawLine(cx, cy+side, cx+line*cos30, cy+side-line*cos60)

    def mousePressEvent(self, event):
        if event.buttons() == Qt.MouseButton.LeftButton:
            self.signalSplashDone.emit()



from PySide6.QtCore    import QRectF
from PySide6.QtWidgets import QApplication, QVBoxLayout
from PySide6.QtWidgets import QGraphicsView, QGraphicsItem
from PySide6.QtWidgets import QGraphicsScene, QGraphicsRectItem
from PySide6.QtCore    import QPropertyAnimation

class Cubelogo(QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)

        self.view = QGraphicsView()
        self.scene = QGraphicsScene()

        self.view.setScene(self.scene)
        self.view.setSceneRect(-200, -200, 400, 400)

        cube = CustomCube(400)
        cube.setPos(0, 0)
        cube.animate()
        self.scene.addItem(cube)

        ct = QVBoxLayout(self)
        ct.setContentsMargins(0, 0, 0, 0)
        ct.addWidget(self.view)
        self.setLayout(ct)


class CustomCube(QGraphicsItem):
    def __init__(self, sidelength):
        super().__init__()
        self.side = sidelength
        self._rotation = 0
        self.setFlag(QGraphicsItem.ItemIsMovable)

    def boundingRect(self):
        return QRectF(-self.side, -self.side, self.side, self.side)

    @Property(int)
    def rotat(self):
        return self._rotation

    @rotat.setter
    def set_rotation(self, angle):
        self._rotation = angle

    def animate(self):
        self.animation = QPropertyAnimation(self, b'rotat')
        self.animation.setDuration(5000)
        self.animation.setStartValue(0)
        self.animation.EndValue(360)
        self.animation.setLoopCount(-1)
        self.animation.start()

    def paint(self, painter, option, widget=None):
        # draw background
        painter.setRenderHint(QPainter.RenderHint.Antialiasing)
        painter.setPen(QPen(Qt.blue, 3))
        width = option.rect.width()
        height = option.rect.height()
        painter.fillRect(0, 0, width, height, QBrush(Qt.white))

        ww = 1.00 * height
        wh = ww
        cx = 0
        cy = 0
        cos30 = 0.866
        cos60 = 0.50
        side = 0.25 * ww
        line = 0.33333 * side

        # Middle Top Part
        painter.drawLine(cx, cy-side, cx-line*cos30, cy-side+line*cos60)
        painter.drawLine(cx, cy-side, cx, cy-side+line)
        painter.drawLine(cx, cy-side, cx+line*cos30, cy-side+line*cos60)
        # Left Top Part
        painter.drawLine(cx-side*cos30, cy-side*cos60, cx-side*cos30+line*cos30, cy-side*cos60-line*cos60)
        painter.drawLine(cx-side*cos30, cy-side*cos60, cx-side*cos30+line*cos30, cy-side*cos60+line*cos60)
        painter.drawLine(cx-side*cos30, cy-side*cos60, cx-side*cos30, cy-side*cos60+line)
        # Right Top Part
        painter.drawLine(cx+side*cos30, cy-side*cos60, cx+side*cos30-line*cos30, cy-side*cos60-line*cos60)
        painter.drawLine(cx+side*cos30, cy-side*cos60, cx+side*cos30-line*cos30, cy-side*cos60+line*cos60)
        painter.drawLine(cx+side*cos30, cy-side*cos60, cx+side*cos30, cy-side*cos60+line)
        # Center Part
        painter.drawLine(cx, cy, cx-line*cos30, cy-line*cos60)
        painter.drawLine(cx, cy, cx, cy+line)
        painter.drawLine(cx, cy, cx+line*cos30, cy-line*cos60)
        # Left Bottom Part
        painter.drawLine(cx-side*cos30, cy+side*cos60, cx-side*cos30, cy+side*cos60-line)
        painter.drawLine(cx-side*cos30, cy+side*cos60, cx-side*cos30+line*cos30, cy+side*cos60-line*cos60)
        painter.drawLine(cx-side*cos30, cy+side*cos60, cx-side*cos30+line*cos30, cy+side*cos60+line*cos60)
        # Right Bottom Part
        painter.drawLine(cx+side*cos30, cy+side*cos60, cx+side*cos30, cy+side*cos60-line)
        painter.drawLine(cx+side*cos30, cy+side*cos60, cx+side*cos30-line*cos30, cy+side*cos60-line*cos60)
        painter.drawLine(cx+side*cos30, cy+side*cos60, cx+side*cos30-line*cos30, cy+side*cos60+line*cos60)
        # Middle Bottom Part
        painter.drawLine(cx, cy+side, cx-line*cos30, cy+side-line*cos60)
        painter.drawLine(cx, cy+side, cx, cy+side-line)
        painter.drawLine(cx, cy+side, cx+line*cos30, cy+side-line*cos60)

        

#app = QApplication()
#win = Cubelogo()
#win.resize(800, 450)
#win.show()
#app.exec()
